package hierarchy

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/request"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/hierarchy"
	hierarchyReq "gitcode.com/G-YT/iHealth25/server/model/hierarchy/request"
	"gitcode.com/G-YT/iHealth25/server/service"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type LevelThreeApi struct {
}

var levelThreeService = service.ServiceGroupApp.HierarchyServiceGroup.LevelThreeService

// CreateLevelThree 创建LevelThree
// @Tags LevelThree
// @Summary 创建LevelThree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelThree true "创建LevelThree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelThree/createLevelThree [post]
func (levelThreeApi *LevelThreeApi) CreateLevelThree(c *gin.Context) {
	var levelThree hierarchy.LevelThree
	err := c.ShouldBindJSON(&levelThree)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Key":   {utils.NotEmpty()},
		"Value": {utils.NotEmpty()},
	}
	if err := utils.Verify(levelThree, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelThreeService.CreateLevelThree(&levelThree); err != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(err))
		response.FailWithMessage("创建失败", c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}

// DeleteLevelThree 删除LevelThree
// @Tags LevelThree
// @Summary 删除LevelThree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelThree true "删除LevelThree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /levelThree/deleteLevelThree [delete]
func (levelThreeApi *LevelThreeApi) DeleteLevelThree(c *gin.Context) {
	var levelThree hierarchy.LevelThree
	err := c.ShouldBindJSON(&levelThree)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	// 检查目录下是否存在文章
	if err := levelThreeService.DeleteLevelThree(levelThree); err != nil {
		global.GVA_LOG.Error("删除失败!", zap.Error(err))
		response.FailWithMessage(err.Error(), c)
	} else {
		response.OkWithMessage("删除成功", c)
	}
}

// DeleteLevelThreeByIds 批量删除LevelThree
// @Tags LevelThree
// @Summary 批量删除LevelThree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body request.IdsReq true "批量删除LevelThree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /levelThree/deleteLevelThreeByIds [delete]
func (levelThreeApi *LevelThreeApi) DeleteLevelThreeByIds(c *gin.Context) {
	var IDS request.IdsReq
	err := c.ShouldBindJSON(&IDS)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelThreeService.DeleteLevelThreeByIds(IDS); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateLevelThree 更新LevelThree
// @Tags LevelThree
// @Summary 更新LevelThree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body hierarchy.LevelThree true "更新LevelThree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /levelThree/updateLevelThree [put]
func (levelThreeApi *LevelThreeApi) UpdateLevelThree(c *gin.Context) {
	var levelThree hierarchy.LevelThree
	err := c.ShouldBindJSON(&levelThree)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	verify := utils.Rules{
		"Key":   {utils.NotEmpty()},
		"Value": {utils.NotEmpty()},
	}
	if err := utils.Verify(levelThree, verify); err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if err := levelThreeService.UpdateLevelThree(levelThree); err != nil {
		global.GVA_LOG.Error("更新失败!", zap.Error(err))
		response.FailWithMessage("更新失败", c)
	} else {
		response.OkWithMessage("更新成功", c)
	}
}

// FindLevelThree 用id查询LevelThree
// @Tags LevelThree
// @Summary 用id查询LevelThree
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchy.LevelThree true "用id查询LevelThree"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /levelThree/findLevelThree [get]
func (levelThreeApi *LevelThreeApi) FindLevelThree(c *gin.Context) {
	var levelThree hierarchy.LevelThree
	err := c.ShouldBindQuery(&levelThree)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if relevelThree, err := levelThreeService.GetLevelThree(levelThree.ID); err != nil {
		global.GVA_LOG.Error("查询失败!", zap.Error(err))
		response.FailWithMessage("查询失败", c)
	} else {
		response.OkWithData(gin.H{"relevelThree": relevelThree}, c)
	}
}

// GetLevelThreeList 分页获取LevelThree列表
// @Tags LevelThree
// @Summary 分页获取LevelThree列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query hierarchyReq.LevelThreeSearch true "分页获取LevelThree列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /levelThree/getLevelThreeList [get]
func (levelThreeApi *LevelThreeApi) GetLevelThreeList(c *gin.Context) {
	var pageInfo hierarchyReq.LevelThreeSearch
	err := c.ShouldBindQuery(&pageInfo)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, total, err := levelThreeService.GetLevelThreeInfoList(pageInfo); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.OkWithDetailed(response.PageResult{
			List:     list,
			Total:    total,
			Page:     pageInfo.Page,
			PageSize: pageInfo.PageSize,
		}, "获取成功", c)
	}
}
